package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.SysUser;
import com.taobao.machinesystem.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@RestController
@RequestMapping("/machine_system/sys-user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/insertSysUser")
    public Result insertSysUser(@RequestBody SysUser user){
        int insert = this.sysUserService.insertSysUser(user);
        if (insert>0){
            //新增成功
            return Result.ok().setMessage(SystemConst.INSERT_SUCCESS);
        }else{
            //新增失败
            return Result.error().setMessage(SystemConst.INSERT_FAIL);
        }
    }

    /**
     * 根据用户ID更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/updateByIdSysUser")
    public Result updateByIdSysUser(@RequestBody SysUser user){
        int update = this.sysUserService.updateByIdSysUser(user);
        if (update>0){
            //更新成功
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        }else{
            //更新失败
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     * 分页查询用户信息
     * @param
     * @return
     */
    @PostMapping("/listSysUser/{currentPage}/{size}")
    public Result listSysUser(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size")Integer size,
            @RequestParam("username") String username){
        Page<SysUser> sysUserPage = this.sysUserService.listSysUser(currentPage, size, username);
        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total",sysUserPage.getTotal())
                .setMapValue("items",sysUserPage.getRecords());
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @PostMapping("/selectByIdSysUser/{id}")
    public Result selectByIdSysUser(@PathVariable("id") String id){
        SysUser sysUser = this.sysUserService.selectByIdSysUser(id);
        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("item",sysUser);
    }

    /**
     * 修改用户的信息
     * @param map
     * @return
     */
    @PostMapping("/updateSysUser")
    public Result updateSysUser(@RequestBody Map<String,String> map){
        System.out.println(map);
        SysUser user = new SysUser();
        user.setId(map.get("id"));
        user.setNickName(map.get("nickName"));
        user.setToken(map.get("token"));
        user.setPassword(map.get("oldPassword"));

        Integer integer = this.sysUserService.updateSysUser(user,map.get("newPassword"));
        if (integer>0){
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        }
        return Result.error().setMessage(SystemConst.UPDATE_FAIL);
    }

    /**
     * 逻辑删除用户
     * @param id
     * @return
     */
    @PostMapping("/isDeleteSysUser/{id}")
    public Result isDeleteSysUser(@PathVariable("id") String id){
        Integer deleteSysUser = this.sysUserService.isDeleteSysUser(id);
        return Result.ok().setMessage(SystemConst.DELETE_SUCCESS);
    }


    @PostMapping("/create/user/role/{userId}")
    public Result createUserRole(@PathVariable("userId") String userId,String[] roleIds){
        boolean createRolePermission =  sysUserService.createUserRole(userId,roleIds);

        if (createRolePermission) {
            return Result.ok().setMessage(SystemConst.INSERT_SUCCESS);
        } else {
            return Result.error().setMessage(SystemConst.INSERT_FAIL);
        }
    }
}

